package com.musicapp;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class Music extends Service {
    public static int currentIndex = 0;
    List<SongModel> songList = FirstFragment.songList;
//    private Runnable mSeekBarRunnable;
    public MediaPlayer mMediaPlayer;
    public static boolean isPlaying = false;
    private int mCurrentPosition = 0;
    private final IBinder mBinder = new LocalBinder();
    private ServiceCallbacks mServiceCallbacks;

    public interface ServiceCallbacks {
        void onPlaybackStateChanged(boolean isPlaying);
        void onPlaybackProgressChanged(int currentPosition);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mMediaPlayer = new MediaPlayer();
        currentIndex = songList.size() - 1 ;
//        Timer timer = new Timer();
//        timer.scheduleAtFixedRate(new TimerTask() {
//            @Override
//            public void run() {
//                if (isPlaying) {
//                    notifyPlaybackProgress();
//                }
//            }
//        }, 0, 300);
    }

//    播放、暂停、停止
    public void play(Uri musicUri,int n ) {
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(getApplicationContext(), musicUri);
            mMediaPlayer.prepare();
            mMediaPlayer.seekTo(mCurrentPosition);
            mMediaPlayer.start();
            isPlaying = true;
            mCurrentPosition = 0;
            notifyPlaybackState();

//            播放模式
//            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//                @Override
//                public void onCompletion(MediaPlayer mp) {
//                    if(n == 1){
//                        currentIndex = (currentIndex - 1  + songList.size()) % songList.size();
//                        mMediaPlayer.start();
//                    }else if(n==2){
//                        currentIndex = (int) (Math.random() * songList.size());
//                        mMediaPlayer.start();
//                    }else {
//                        mMediaPlayer.seekTo(0);
//                        mMediaPlayer.start();
//                    }
//                }
//            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void pause() {
        if (isPlaying) {
            mCurrentPosition = mMediaPlayer.getCurrentPosition();
            mMediaPlayer.pause();
            isPlaying = false;
            notifyPlaybackState();
        }
    }
    public void stop() {
        if (isPlaying) {
            mCurrentPosition = 0;
            mMediaPlayer.stop();
            mMediaPlayer.reset();
            isPlaying = false;
            notifyPlaybackState();
        }
    }

//    进度条跳转
//    public void seekTo(int position) {
//        if (isPlaying) {
//            mMediaPlayer.seekTo(position);
//            mMediaPlayer.start();
//            mCurrentPosition = position;
//            notifyPlaybackState();
//        }
//    }

//    歌曲时长
//    public int getDuration() {
//        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
//            return mMediaPlayer.getDuration();
//        } else {
//            return 0;
//        }
//    }

//    当前时长
    public int getCurrent() {
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            return mMediaPlayer.getCurrentPosition();
        } else {
            return 0;
        }
    }

//    private void notifyPlaybackProgress() {
//        if (mServiceCallbacks != null) {
//            mServiceCallbacks.onPlaybackProgressChanged(mMediaPlayer.getCurrentPosition());
//            if (mSeekBarRunnable != null) {
//                mSeekBarRunnable.run();
//            }
//        }
//    }

    private void notifyPlaybackState() {
        if (mServiceCallbacks != null) {
            mServiceCallbacks.onPlaybackStateChanged(isPlaying);
        }
    }

//    切歌
    public void playPreviousSong(int n) {
        if (!songList.isEmpty()) {
            if(n==2){
                currentIndex = (int) (Math.random() * songList.size());
            }else {
                currentIndex = (currentIndex - 1  + songList.size()) % songList.size();
            }
            stop();

            Uri musicUri = Uri.parse(songList.get(currentIndex).getMusicurl());
            play(musicUri,n);
        }
    }

    public void playNextSong(int n ) {
        if (!songList.isEmpty()) {
            stop();
            if(n==2){
                currentIndex = (int) (Math.random() * songList.size());
            }else {
                currentIndex = (currentIndex + 1  + songList.size()) % songList.size();
            }
            Uri musicUri = Uri.parse(songList.get(currentIndex).getMusicurl());
            play(musicUri,n);
        }
    }

//    public void setSeekBarCallback(Runnable runnable) {
//        mSeekBarRunnable = runnable;
//    }

    public class LocalBinder extends Binder {
        Music getService() {
            return Music.this;
        }

        void registerCallbacks(ServiceCallbacks callbacks) {
            mServiceCallbacks = callbacks;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public void onDestroy() {
        super.onDestroy();
    }
}